var _ctFfmpegV2A = null; var _ctV2AFormats = { mp3: { codec: ['-c:a', 'libmp3lame'], lossy: true, mime: 'audio/mpeg' }, m4a: { codec: ['-c:a', 'aac'], lossy: true, mime: 'audio/mp4' }, ogg: { codec: ['-c:a', 'libvorbis'], lossy: true, mime: 'audio/ogg' }, wav: { codec: ['-c:a', 'pcm_s16le'], lossy: false, mime: 'audio/wav' }, flac: { codec: ['-c:a', 'flac'], lossy: false, mime: 'audio/flac' } }; function _ctEnsureFfmpegV2A() { if (_ctFfmpegV2A) return Promise.resolve(_ctFfmpegV2A); return loadScriptPromise('https://unpkg.com/@ffmpeg/ffmpeg@0.11.6/dist/ffmpeg.min.js').then(function() { var ns = window.FFmpeg || {}; if (!ns.createFFmpeg) throw new Error('FFmpeg library failed to load'); var ffmpeg = ns.createFFmpeg({ log: false, corePath: 'https://unpkg.com/@ffmpeg/core@0.11.0/dist/ffmpeg-core.js' }); _ctFfmpegV2A = { ffmpeg: ffmpeg, fetchFile: ns.fetchFile }; return ffmpeg.load().then(function(){ return _ctFfmpegV2A; }); }); } function _ctV2AStatus(msg) { var el = document.getElementById('v2a-status'); if (el) el.textContent = msg || ''; } function _ctV2AToggleBitrate() { var fmt = _ctV2AFormats[$('#v2a-format').val()] || _ctV2AFormats.mp3; $('#v2a-bitrate-wrap').toggle(fmt.lossy); } $(function() { $('#v2a-format').on('change', _ctV2AToggleBitrate); _ctV2AToggleBitrate(); }); function processFile(blob, fileName) { var key = $('#v2a-format').val(); var fmt = _ctV2AFormats[key] || _ctV2AFormats.mp3; if (!_ctV2AFormats[key]) key = 'mp3'; var bitrate = parseInt($('#v2a-bitrate').val(), 10) || 192; var ext = ((fileName || '').toLowerCase().split('.').pop() || 'mp4'); var inName = 'in.' + ext; var outName = 'out.' + key; var args = ['-i', inName, '-vn', '-map', '0:a:0'].concat(fmt.codec); if (fmt.lossy) args = args.concat(['-b:a', bitrate + 'k']); args.push(outName); _ctV2AStatus('Loading converter...'); _ctEnsureFfmpegV2A().then(function(ctx) { var ffmpeg = ctx.ffmpeg, fetchFile = ctx.fetchFile; _ctV2AStatus('Extracting audio...'); return fetchFile(blob).then(function(buf) { try { ffmpeg.FS('unlink', inName); } catch (e) {} try { ffmpeg.FS('unlink', outName); } catch (e) {} ffmpeg.FS('writeFile', inName, buf); return ffmpeg.run.apply(ffmpeg, args).then(function() { var data; try { data = ffmpeg.FS('readFile', outName); } catch (e) {} if (!data || !data.length) { _ctV2AStatus(''); alert('This video does not seem to have an audio track.'); return; } var outBlob = new Blob([data.buffer], { type: fmt.mime }); var base = (fileName || 'audio').replace(/\.[^.]+$/, ''); add_file_output(URL.createObjectURL(outBlob), base + '.' + key); _ctV2AStatus(''); try { ffmpeg.FS('unlink', inName); } catch (e) {} try { ffmpeg.FS('unlink', outName); } catch (e) {} }); }); }).catch(function(err) { _ctV2AStatus(''); alert('Could not extract audio from this video: ' + (err && err.message || err) + '. The first run downloads ~30 MB of conversion code - please check your connection and try again.'); }); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }